package io.renren.com.qywt.controller;

import io.renren.commons.log.annotation.LogOperation;
import io.renren.commons.tools.constant.Constant;
import io.renren.commons.tools.page.PageData;
import io.renren.commons.tools.utils.Result;
import io.renren.commons.tools.utils.ExcelUtils;
import io.renren.commons.tools.validator.AssertUtils;
import io.renren.commons.tools.validator.ValidatorUtils;
import io.renren.commons.tools.validator.group.AddGroup;
import io.renren.commons.tools.validator.group.DefaultGroup;
import io.renren.commons.tools.validator.group.UpdateGroup;
import io.renren.com.qywt.dto.AftersaleChanageRecordDTO;
import io.renren.com.qywt.excel.AftersaleChanageRecordExcel;
import io.renren.com.qywt.service.AftersaleChanageRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 客服工单变更记录表
*
* @author jiaohj 15110414840@139.com
* @since 3.0 2022-09-20
*/
@RestController
@RequestMapping("record")
@Api(tags="客服工单变更记录表")
public class AftersaleChanageRecordController {
    @Autowired
    private AftersaleChanageRecordService aftersaleChanageRecordService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<AftersaleChanageRecordDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AftersaleChanageRecordDTO> page = aftersaleChanageRecordService.page(params);
        return new Result<PageData<AftersaleChanageRecordDTO>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("变更信息列表")
    public Result<List<AftersaleChanageRecordDTO>> list(@RequestParam Map<String, Object> params){
        List<AftersaleChanageRecordDTO> list = aftersaleChanageRecordService.getByOrderId(params);
        return new Result<List<AftersaleChanageRecordDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<AftersaleChanageRecordDTO> get(@PathVariable("id") Long id){
        AftersaleChanageRecordDTO data = aftersaleChanageRecordService.get(id);
        return new Result<AftersaleChanageRecordDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody AftersaleChanageRecordDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        aftersaleChanageRecordService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody AftersaleChanageRecordDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        aftersaleChanageRecordService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        aftersaleChanageRecordService.delete(ids);
        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AftersaleChanageRecordDTO> list = aftersaleChanageRecordService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, "客服工单变更记录表", list, AftersaleChanageRecordExcel.class);
    }

}